<template>
  <div class="pro_type_add_edit">
    <el-dialog
      :title="typeData.title"
      :visible.sync="showView"
      @close="dialogClose"
      width="30%"
      class="dialog_pro_type"
    >
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" @submit.native.prevent label-width="80px" class="demo-ruleForm">
        <el-form-item :label="typeData.label" prop="name">
          <el-input type="text" size="small"  v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sortIndex">
          <el-input-number controls-position="right" size="small" v-model="ruleForm.sortIndex" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
        <el-button type="primary" native-type="submit"  size="small"  @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapActions, mapGetters, mapMutations } from "vuex";
export default {
  name: "pro-type-add-edit",
  props:{
    type:{
      type:String,
      default:''
    }
  },
  data(){
    return {
      showView:false,
      typeData:{},
      ruleForm:{
        name:'',
        sortIndex:0,
      },
      rules:{
        name: [
          { required: true, message: '请输入类别名称', trigger: 'blur' }
        ],
        
      }
    }
  },
  created(){
  },
  computed:{
    ...mapGetters({
      userInfo:"getUserInfo",
    }),
    
  },
  methods:{
    // ...mapMutations({
    // }),
    //模态框打开时
    open(data){
      this.typeData=data;
      // console.log(data,this.type,'edit');
      if(this.type == 'edit'){
        this.ruleForm={
          name:data.opt.name,
          sortIndex:data.opt.sortIndex
        }
        this.showView=true;
      }else{
        this.showView=true;
        this.$nextTick(()=>{
          this.$refs['ruleForm'].resetFields();     //需要dom渲染出来之后才能找到ref元素
        })
      }
    },
    //模态框关闭时
    dialogClose(){
      this.$refs['ruleForm'].resetFields();
      this.typeData={};
    },
    getUUID(){
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      }
      return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    },
    submitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //这里调接口然后刷新 假数据直接调用父组件方法添加进去
          if(this.type=='add'){
            this.ruleForm.id=this.getUUID();
            this.$emit('add',this.ruleForm);
          }else if(this.type=='edit'){
            let opt={
              name:this.ruleForm.name,
              sortIndex:this.ruleForm.sortIndex,
              id:this.typeData.opt.id
            }
            //编辑得需要通过id找到对应得数据并替换掉
            this.$emit('edit',opt);
          }else if(this.type == 'addChildren'){
            let opt={
              id:this.getUUID(),
              name:this.ruleForm.name,
              sortIndex:this.ruleForm.sortIndex
            }
            this.$emit('addChildren',opt,this.typeData.opt.id);
          }
          this.showView=false;
        } else {
          //校验规则不通过 
          return false;
        }
      });
    }
  },
}
</script>
<style lang="scss" scoped>

</style>